<template>
    <div>
        <div class="register_con">
            <div class="l_con fl">
                <a class="reg_logo"><img src="/static/images/logo.png"></a>
                <div class="reg_slogan">商品美 · 种类多 · 欢迎光临</div>
                <div class="reg_banner"></div>
            </div>

            <div class="r_con fr">
                <div class="reg_title clearfix">
                    <h1>用户注册</h1>
                    <a href="/login">登录</a>
                </div>
                <div class="reg_form clearfix" id="app" v-cloak>
                    <form @submit.prevent="register">
                        <ul>
                            <li>
                                <label>用户名:</label>
                                <input type="text" v-model="username" name="user_name" @blur="check_username" id="user_name">
                                <span v-show="error_name" class="error_tip">用户已存在</span>
                            </li>
                            <li>
                                <label>密码:</label>
                                <input type="password" v-model="password1" name="pwd" id="pwd">
                                <!-- <span v-show="error_password" class="error_tip">密码最少8位，最长20位</span> -->
                            </li>
                            <li>
                                <label>确认密码:</label>
                                <input type="password" v-model="password2" name="cpwd" id="cpwd">
                                <!-- <span v-show="error_check_password" class="error_tip">两次输入的密码不一致</span> -->
                            </li>
                            <li>
                                <label>邮箱:</label>
                                <input type="text"  v-model="email" >
                                <!-- <span v-show="error_phone" class="error_tip">您输入的手机号格式不正确</span> -->
                            </li>
                            <li>
                                <label>验证码:</label>
                                <input type="text" v-model="image_code" name="msg_code" @blur="check_code" id="msg_code" class="msg_input">
                                <img :src="img_url + image_id" alt="" @click="get_uuid" style="width: 120px; height: 40px">
                                <span v-show="error_code" class="error_tip">验证码输入有误，请重新输入</span>
                            </li>
                            <li class="agreement">
                                <input type="checkbox" v-model="allow"  id="allow" checked="checked">
                                <label>同意”美多商城用户使用协议“</label>
                                <!-- <span v-show="error_allow" class="error_tip2">请勾选同意</span> -->
                            </li>
                            <li class="reg_sub">
                                <input type="submit" value="注 册" name="">
                            </li>
                        </ul>
                    </form>
                </div>

            </div>

        </div>

        <myfooder></myfooder>
    </div>
</template>

<script>
import axios from 'axios'
import myfooder from '../components/Fooder'

export default {
    components: {
        'myfooder': myfooder
    },
    data() {
        return {
            username: '',
            password1: '',
            password2: '',
            allow: '1',
            email: '',
            base_url: 'http://127.0.0.1:8000',
            img_url: 'http://127.0.0.1:8000/app01/verify_code/?image_id=',
            image_id: '',
            image_code: '',
            // 错误提示
            error_name: false,
            error_code: false
        }
    },
    methods: {
        check_code() {
            axios({
                url: this.base_url + '/app01/check_code/',
                params: {'image_id': this.image_id, 'image_code': this.image_code}
            }).then(res => {
                console.log(res.data)
                if(res.data.code == 400) {
                    this.error_code = true
                }else{
                    this.error_code = false
                }
            })
        },
        // 获取uuid
        get_uuid() {
            // 发送get请求，获取uuid
            axios({
                url: this.base_url + '/app01/create_uuid/'
            }).then(res => {
                console.log(res.data)
                this.image_id = res.data.image_id
            })
        },
        // 检查用户名
        check_username() {
            axios({
                url: this.base_url + '/app01/check_username/?username=' + this.username
            }).then(res => {
                console.log(res.data)
                if(res.data.code == 200) {
                    this.error_name = false
                }else{
                    this.error_name = true
                }
            })
        },
        // 注册
        register() {
            // 1、实例化
            let form_data = new FormData()
            // 2、添加数据
            form_data.append('username', this.username)
            form_data.append('password1', this.password1)
            form_data.append('password2', this.password2)
            form_data.append('email', this.email)
            // 3、发送数据
            axios({
                url: this.base_url + '/app01/register/',
                method: 'post',
                data: form_data
            }).then(res => {
                console.log(res)
                console.log(res.data)
            })

        }
    },
    created() {
        // 页面载时，调用uuid方法
        this.get_uuid()
    }
}
</script>

<style scoped>

</style>
